﻿using Infra_Common.redis;
using MediatR;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using xiaodingmarket_Domain.Commands.User;
using xiaodingmarket_Domain.Interfaces;
using xiaodingmarket_Domain.Interfaces.IRepository;
using xiaodingmarket_Domain.Models;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Domain_core.Events;
using xiaodingmarket_Domain_core.Notificatiions;

namespace xiaodingmarket_Domain.CommandHandlers
{
    public class UserCommandHandler : CommandHandler,
         IRequestHandler<CreateUserCommand, bool>,  //Unit来代表无返回的情况
         IRequestHandler<UpdateUserCommand, bool>,
         IRequestHandler<RemoveUserCommand, bool>,
         IRequestHandler<BeAdminCommand, bool>
    {

        // 注入仓储接口
        private readonly IUserRepository _UserRepository;
        //事件溯源
        private readonly IEventStoreService _EventStoreService;
        // 注入总线
        private readonly IMediatorHandler Bus;
        //注入redis
        private readonly RedisClient redis;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="studentRepository"></param>
        /// <param name="uow"></param>
        /// <param name="bus"></param>
        /// <param name="cache"></param>
        public UserCommandHandler(IUserRepository UserRepository,
                                      IEventStoreService EventStoreService,
                                      IUnitOfWork uow,
                                      IMediatorHandler bus,
                                      INotificationHandler<DomainNotification> notifications,
                                      RedisClient redisclient
                                      ) : base(uow, bus, notifications)
        {
            _UserRepository = UserRepository;
            _EventStoreService = EventStoreService;
            Bus = bus;
            redis = redisclient;
        }

        public Task<bool> Handle(CreateUserCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return Task.FromResult(false);
            }
            Guid id = new Guid();
            List<UserRoles> userroles = new List<UserRoles>
            {
                new UserRoles{UserID=id, RoleID =new Guid("5F668E64-8956-BA8E-67EA-503D01089EB1"),IsDelete=false }
            };
            User user = new User(
                id,message.StudentId,
                message.StudentName,message.StudentSex,
                message.collegename,message.Userid,
                message.StudentName,message.UserPictureSrc,"",
                DateTime.Now, DateTime.Now, DateTime.Now,"","",false,""
                );

            _UserRepository.Add(user);

            if (Commit())
            {
                //Bus.RaiseEvent();
            }

            return Task.FromResult(true);
        }

        public Task<bool> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<bool> Handle(RemoveUserCommand request, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task<bool> Handle(BeAdminCommand request, CancellationToken cancellationToken)
        {
            var user=_UserRepository.Getuserrole(request.studentid);
            user.userroles.Add(new UserRoles() { UserID = user.Id, RoleID=new Guid() });
            if (Commit())
            {
            }
            return Task.FromResult(true);
        }
    }
}
